import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Node {
	String name;
	ArrayList<String> keyWords;
	ArrayList<Node> subCategory;
	boolean isLeaf;

	private Node(String inputName) {
		name = inputName;
		keyWords = new ArrayList<String>();
		subCategory = new ArrayList<Node>();
		isLeaf = false;
	}

	public static Node creatCategoryTree(String categoryName) {
		return readFile(categoryName, new Node(categoryName));
	}

	public static Node findByName(String name, Node root) {
		if (root.name.equals(name))
			return root;

		for (Node sub : root.subCategory) {
			Node result = findByName(name, sub);
			if (result != null)
				return result;
		}

		return null;
	}

	private static Node readFile(String fileName, Node root) {
		File f = new File(fileName + ".txt");
		if (f.exists()) {
			try {
				FileInputStream fstream = new FileInputStream(fileName + ".txt");
				DataInputStream in = new DataInputStream(fstream);
				BufferedReader br = new BufferedReader(
						new InputStreamReader(in));
				readLineByLine(root, br);
				br.close();

				for (int i = 0; i < root.subCategory.size(); i++) {
					String nextFile = root.subCategory.get(i).name;
					readFile(nextFile, root.subCategory.get(i));
				}

			} catch (Exception e) {// Catch exception if any
				System.err.println("Error: " + e.getMessage());
			}
		} else {
			root.isLeaf = true;
		}

		return root;
	}

	private static void readLineByLine(Node root, BufferedReader br)
			throws IOException {
		String subName = "";
		String strLine;

		// TODO Auto-generated method stub
		while ((strLine = br.readLine()) != null) {
			String[] strSep = strLine.split(" ", 2);
			if (subName.equals(strSep[0].toLowerCase()))
				root.subCategory.get(root.subCategory.size() - 1).keyWords
						.add(strSep[1].trim());
			else {
				subName = strSep[0].toLowerCase();
				Node subCat = new Node(subName);
				subCat.keyWords.add(strSep[1]);
				root.subCategory.add(subCat);
			}
		}
	}

	/*
	 * public static void main(String[] args) { Node result = readFile("root",
	 * new Node("root")); System.out.println(result.name + result.isLeaf); for
	 * (int i = 0; i < result.subCategory.size(); i++) {
	 * System.out.println(result.subCategory.get(i).name +
	 * result.subCategory.get(i).isLeaf); for (int j = 0; j <
	 * result.subCategory.get(i).subCategory.size(); j++) System.out
	 * .println(result.subCategory.get(i).subCategory.get(j).name +
	 * result.subCategory.get(i).subCategory.get(j).isLeaf);
	 * 
	 * }
	 * 
	 * }
	 */
	/*
	 * public static void main(String[] args) { Node result = readFile("root",
	 * new Node("root")); System.out.println(result.name);
	 * System.out.println(result.subCategory.size()); for (int i = 0; i <
	 * result.subCategory.size(); i++) {
	 * System.out.println(result.subCategory.get(i).name);
	 * System.out.println(result.subCategory.get(i).subCategory.size()); for
	 * (int j = 0; j < result.subCategory.get(i).keyWords.size(); j++) {
	 * System.out.println(result.subCategory.get(i).keyWords.get(j));
	 * 
	 * } System.out.println("END");
	 * 
	 * for (int m = 0; m < result.subCategory.get(i).subCategory.size(); m++) {
	 * System.out .println(result.subCategory.get(i).subCategory.get(m).name);
	 * for (int n = 0; n < result.subCategory.get(i).subCategory
	 * .get(m).keyWords.size(); n++) {
	 * System.out.println(result.subCategory.get(i).subCategory
	 * .get(m).keyWords.get(n)); } System.out.println("SubEND"); } } }
	 */
}
